package cn.oa.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import cn.oa.bean.Account;
import cn.oa.bean.Log;
import cn.oa.service.LogService;

@Controller
public class LogController {
	@Resource
	LogService logService;
	@PostMapping("/addLog")
	public String addAnn(ModelMap map,HttpServletRequest req,Log log){
		System.out.println(log);
		HttpSession session = req.getSession();
		Account account = (Account) session.getAttribute("Account");
		System.out.println(account);
		log.setUserId(account.getId());
		Date data = new Date();
		log.setCreateTime(data);
		System.out.println(log);
		map.put("insert", logService.addLog(log));
		return "/log_add";
	}
	
	
	@GetMapping("/Log")
	public String allAnn(ModelMap map,HttpServletRequest req){
		HttpSession session = req.getSession();
		Account account = (Account) session.getAttribute("Account");
		System.out.println(account);
		String position = logService.query(account);
		if("老总".equals(position)){
			map.put("DeLog",logService.findDeLog((Account)account));
			map.put("result",logService.findAllLog());
			map.addAttribute("errorMessage", "你有权查看所有公告");
		}else if("部门经理".equals(position)){
			map.put("DeLog",logService.findDeLog((Account)account));
			map.addAttribute("errorMessage", "你只有权查看部门公告");
		}else{
			map.addAttribute("errorMessage", "你无权查看公告");
		}
		return "/logindex";
	}
}
